﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Gmis.Public;
using System.IO;

namespace Gmis.Web.Manage.Village
{
    /// <summary>
    /// UploadImage1 的摘要说明
    /// </summary>
    public class UploadImageHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";

            string sId = context.Request["id"];
            string action = context.Request["action"];
            //
            if (string.IsNullOrEmpty(action))
            {
                context.Response.Write(CommonFunction.failure("无指定操作"));
            }
            else
            {
                switch (action.ToLower())
                {
                    case "add":
                        context.Response.Write(add(context));
                        break;
                    case "edit":
                        context.Response.Write(update());
                        break;
                    case "delete":
                        context.Response.Write(delete(sId));
                        break;
                    case "deleteall":
                        context.Response.Write(deleteAll(context.Request["ids"]));
                        break;
                }
            }
        }

        private Model.NewsFile GenerateModel()
        {
            //获取form数据
            Model.NewsFile model = new Model.NewsFile();
            model.sId = CommonFunction.getRequestValue("txt_sId");
            if (model.sId == "") model.sId = CommonFunction.GetGuid();//
            model.sTitle = CommonFunction.getRequestValue("txt_sTitle");// context.Request["txt_sName"];
            model.dDate = Convert.ToDateTime(CommonFunction.getRequestValue("txt_dDate"));
            model.sDescription = CommonFunction.getRequestValue("txt_sContent");
            model.sType = CommonFunction.getRequestValue("txt_sType");
            return model;
        }

        //增加一条
        private string add(HttpContext context)
        {
            BLL.NewsFile bll = new BLL.NewsFile();
             
            HttpPostedFile file = context.Request.Files["file_Upload"];//获取文件
            Model.NewsFile model = GenerateModel();
            model.sFormat = Path.GetExtension(file.FileName);//获取文件格式
            model.sFileName = Path.GetFileName(file.FileName);
                
            model.sSavePath = DateTime.Now.ToString("yyyy-MM-dd").Replace("-", "");
            string rootPath = context.Server.MapPath("~/");
            string filePath = rootPath;
            //          
            if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);
            filePath += "UploadFile/";
            if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);
            if (model.sType == "image")
            {
                filePath += "Image";
            }
            else
            {
                filePath += "Video";
            }
            if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);
            filePath += "/" + model.sSavePath;//相对路径
            if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);
            filePath += "/" + model.sId + Path.GetExtension(model.sFormat);
            try
            {
                file.SaveAs(filePath);
            }
            catch
            {
                string data = "{\"statusCode\":\"300\",\"message\":\"文件上传失败\",\"navTabId\":\"\",\"rel\":\"\",\"callbackType\":\"closeCurrent\",\"forwardUrl\":\"Village/UploadImage.aspx\"}";
     
                return data;
            }

            if (bll.Add(model))
            {
                //if (model.sType == "image")
                //{
                //    return CommonFunction.success("", "UploadImage");
                //}
                //else
                //{
                //    return CommonFunction.success("", "UploadVideo");
                //}
                if (model.sType == "image")
                {
                    return CommonFunction.successAndCloseCurrent("", "UploadImage", "UploadImage");
                }
                else
                {
                    return CommonFunction.successAndCloseCurrent("", "UploadVideo", "UploadVideo");
                }
              
            }
            else
            {
                string data = "{\"statusCode\":\"300\",\"message\":\"数据保存失败\",\"navTabId\":\"\",\"rel\":\"\",\"callbackType\":\"closeCurrent\",\"forwardUrl\":\"\"}";
                return data;
              
            }
        }

        //删除一条
        private string delete(string sId)
        {
            BLL.NewsFile bll = new BLL.NewsFile();
            if (bll.Delete(sId))
            {
                return CommonFunction.success();
            }
            else
            {
                return CommonFunction.failure();
            }
        }

        //删除全部
        private string deleteAll(string ids)
        {
            string[] strs = ids.Split(',');
            try
            {
                BLL.NewsFile bll = new BLL.NewsFile();
                foreach (string str in strs)
                {
                    bll.Delete(str);
                }
                return CommonFunction.success();
            }
            catch { return CommonFunction.failure(); } 

        }


        //更新数据
        private string update()
        {
            BLL.NewsFile bll = new BLL.NewsFile();
            Model.NewsFile model = GenerateModel();
            if (bll.Update(model))
            {
                if (model.sType == "image")
                {
                    return CommonFunction.successAndCloseCurrent("", "UploadImage", "UploadImage");
                }
                else
                {
                    return CommonFunction.successAndCloseCurrent("", "UploadVideo", "UploadVideo");
                }                
            }
            else
            {
                return CommonFunction.failure();
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}